/**
 * 路由权限配置
 */

// 公开路由 - 不需要登录即可访问
export const PUBLIC_ROUTES = [
  '/login'
];

// 受保护路由 - 需要登录才能访问
export const PROTECTED_ROUTES = [
  '/',
  '/home',
  '/experts',
  '/cart',
  '/user',
  '/orders',
  '/orders/detail/:orderNo',
  '/settings',
  '/products'
];

// 路由配置
export const ROUTE_CONFIG = {
  // 公开路由
  PUBLIC: {
    '/login': {
      title: '登录',
      requireAuth: false,
      component: 'Login',
      redirectWhenLoggedIn: '/home' // 已登录时重定向到首页
    }
  },
  
  // 受保护路由
  PROTECTED: {
    '/': {
      title: '首页',
      requireAuth: true,
      component: 'Index',
      roles: [] // 允许所有角色
    },
    '/home': {
      title: '首页',
      requireAuth: true,
      component: 'Layout',
      roles: []
    },
    '/products': {
      title: '全部商品',
      requireAuth: true,
      component: 'Layout',
      roles: []
    },
    '/experts': {
      title: '专家|技师',
      requireAuth: true,
      component: 'Layout',
      roles: []
    },
    '/cart': {
      title: '购物车',
      requireAuth: true,
      component: 'Layout',
      roles: []
    },
    '/user': {
      title: '我的',
      requireAuth: true,
      component: 'Layout',
      roles: []
    },
    '/orders': {
      title: '我的订单',
      requireAuth: true,
      component: 'Orders',
      roles: []
    },
    '/orders/detail/:orderNo': {
      title: '订单详情',
      requireAuth: true,
      component: 'OrderDetail',
      roles: []
    },
    '/settings': {
      title: '设置',
      requireAuth: true,
      component: 'Settings',
      roles: []
    },
    '/products': {
      title: '全部商品',
      requireAuth: true,
      component: 'Products',
      roles: []
    }
  }
};

// 检查路由是否为公开路由
export const isPublicRoute = (pathname) => {
  return PUBLIC_ROUTES.includes(pathname);
};

// 检查路由是否为受保护路由
export const isProtectedRoute = (pathname) => {
  return PROTECTED_ROUTES.includes(pathname);
};

// 获取路由配置
export const getRouteConfig = (pathname) => {
  return ROUTE_CONFIG.PUBLIC[pathname] || ROUTE_CONFIG.PROTECTED[pathname] || null;
};

// 检查路由是否需要登录
export const requiresAuth = (pathname) => {
  const config = getRouteConfig(pathname);
  return config ? config.requireAuth : true; // 默认需要登录
};

// 获取路由标题
export const getRouteTitle = (pathname) => {
  const config = getRouteConfig(pathname);
  return config ? config.title : '未知页面';
};

// 检查用户是否有权限访问路由
export const hasRoutePermission = (pathname, userRole = null) => {
  const config = getRouteConfig(pathname);
  if (!config) return false;
  
  // 如果路由不需要认证，直接允许
  if (!config.requireAuth) return true;
  
  // 如果路由没有角色限制，允许所有用户
  if (!config.roles || config.roles.length === 0) return true;
  
  // 检查用户角色
  return userRole && config.roles.includes(userRole);
};

// 获取登录后的重定向路径
export const getRedirectPathAfterLogin = (pathname) => {
  const config = getRouteConfig(pathname);
  return config && config.redirectWhenLoggedIn ? config.redirectWhenLoggedIn : '/home';
};

// 获取所有路由配置
export const getAllRoutes = () => {
  return {
    ...ROUTE_CONFIG.PUBLIC,
    ...ROUTE_CONFIG.PROTECTED
  };
};

// 获取需要认证的路由列表
export const getProtectedRoutes = () => {
  return Object.keys(ROUTE_CONFIG.PROTECTED);
};

// 获取公开路由列表
export const getPublicRoutes = () => {
  return Object.keys(ROUTE_CONFIG.PUBLIC);
};
